package cn.wolfcode.shop.cloud;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Result<T> {

    public static final int SUCCESS_CODE = 200;
    public static final String SUCCESS_MSG = "ok";

    public static final int DEFAULT_ERROR_CODE = 50001;
    public static final String DEFAULT_ERROR_MSG = "system error";

    //状态码
    private int code;
    //消息
    private String msg;
    //返回值
    private T data;

    public Result() {
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    //成功
    public static <T> Result success(T data) {
        return new Result(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    //失败
    public static Result error(CodeMsg codeMsg) {
        return new Result(codeMsg.getCode(), codeMsg.getMsg(), null);
    }

    //默认失败
    public static <T> Result defaultError() {
        return new Result(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MSG, null);
    }
}
